package com.paic.mhis.hcpms.assessment.biz.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.paic.mhis.common.base.intergration.dao.CommonDAO;
import com.paic.mhis.hcpms.assessment.biz.service.ExportWordService;
import com.paic.mhis.hcpms.assessment.dto.CodeAndScore;
import com.paic.mhis.hcpms.assessment.dto.CodeNameAndScore;
import com.paic.mhis.hcpms.evaluation.intergration.sao.FwaOrgSao;

/**
 * 
 * Description:导出word从后台获取数据的相关方法
 * 
 * @author: EX-TIANZINONG001
 * @version:
 */
@Service("exportWordService")
public class ExportWordServiceImpl implements ExportWordService {

	@Autowired
	private CommonDAO commonDAO;

	@Resource
	private FwaOrgSao fwaOrgSao;

	/**
	 * 
	 * Description:
	 * 
	 * @param coding
	 *            (key) 医院编码
	 * @return Map<String, Object> 返回当前医院的基本信息 coding 医院编码
	 * 
	 *         Modification History: Date Version Author Description
	 *         ------------
	 *         ------------------------------------------------------ 2015-3-11
	 *         mhis_hcpms EX-TIANZINONG001 创建
	 */
	@Override
	public Map<String, Object> getHospitalInfo(String coding) {
		Object obj = commonDAO.get("hospitalOrgInfo.query", coding);
		Map<String, Object> map = null;
		if (null != obj) {
			map = (Map<String, Object>) obj;
			if (!"2".equals(map.get("HOSLEVEL")) && !"3".equals(map.get("HOSLEVEL"))) {
				map.remove("HOSLEVEL");
				map.put("HOSLEVEL", "4");
			}
		}
		return map;
	}

	/**
	 * 
	 * Description: 返回总得分
	 * 
	 * @param map
	 *            (key) coding 医院编码 nowDate 查询时间
	 * @return float 得分
	 * 
	 *         Modification History: Date Version Author Description
	 *         ------------
	 *         ------------------------------------------------------ 2015-3-11
	 *         mhis_hcpms EX-TIANZINONG001 创建
	 */
	@Override
	public float getTotal(Map<String, Object> map) {
		List<Map<String, Object>> totalScoreList = commonDAO.query("totalScore.query", map);
		if (!CollectionUtils.isEmpty(totalScoreList) && totalScoreList.get(0).get("TOTALSCORE") != null) {
			return Float.parseFloat(totalScoreList.get(0).get("TOTALSCORE") + "");
		}
		return 0;
	}

	/**
	 * 
	 * Description:获得本月 上月 及同等级机构一级指标的得分
	 * 
	 * @param map
	 *            医院编码coding 查询时间(当前月份nowDate还有上个月份lastDate) 指标体系编号 structureNO
	 * @return List 得分的集合
	 * 
	 *         Modification History: Date Version Author Description
	 *         ------------
	 *         ------------------------------------------------------ 2015-3-11
	 *         mhis_hcpms EX-TIANZINONG001 创建
	 */
	@Override
	public List<CodeAndScore> getindicatorCodeAndScore(Map<String, Object> map) {
		List<CodeAndScore> list = commonDAO.query("IndicatorCodeScore.query", map);
		return list;
	}

	/**
	 * 
	 * Description: 根据kpiTime查询全市所有医院的总数
	 * 
	 * @param mapParamter
	 *            查询的时间 nowDate yyyy-MM
	 * @return int
	 * 
	 *         Modification History: Date Version Author Description
	 *         ------------
	 *         ------------------------------------------------------ 2015-3-11
	 *         mhis_hcpms EX-TIANZINONG001 创建
	 */
	@Override
	public int getCityCount(Map<String, Object> mapParamter) {
		// 一个参数
		int cityCount = commonDAO.count("cityCount.query", mapParamter);
		return cityCount;
	}

	/**
	 * 
	 * Description: 当前得分在全市等分中的排名
	 * 
	 * @param mapParamter
	 *            coding 医院编码 和 查询时间 nowDate yyyy-MM
	 * @return int
	 * 
	 *         Modification History: Date Version Author Description
	 *         ------------
	 *         ------------------------------------------------------ 2015-3-11
	 *         mhis_hcpms EX-TIANZINONG001 创建
	 */
	@Override
	public int getCityRowNum(Map<String, Object> mapParamter) {
		// 两个参数
		int cityRowNum = commonDAO.count("cityRownum.query", mapParamter);
		return cityRowNum;
	}

	/**
	 * 
	 * Description: 返回本月同等级医院的总数
	 * 
	 * @param mapParamter
	 *            nowDate(key)
	 * @return int(总数)
	 * 
	 *         Modification History: Date Version Author Description
	 *         ------------
	 *         ------------------------------------------------------ 2015-3-11
	 *         mhis_hcpms EX-TIANZINONG001 创建
	 */
	@Override
	public int getGradeCount(Map<String, Object> mapParamter) {
		// 两个参数
		int gradeCount = commonDAO.count("gradeCount.query", mapParamter);
		return gradeCount;
	}

	/**
	 * 
	 * Description: 返回本月同级别医院排名
	 * 
	 * @param mapParamter
	 *            (key)coding 医院编码 和 查询时间 nowDate yyyy-MM
	 * @return int
	 * 
	 *         Modification History: Date Version Author Description
	 *         ------------
	 *         ------------------------------------------------------ 2015-3-11
	 *         mhis_hcpms EX-TIANZINONG001 创建
	 */
	@Override
	public int getGradeRowNum(Map<String, Object> mapParamter) {
		// 两个参数
		int gradeRowNum = commonDAO.count("gradeRownum.query", mapParamter);
		return gradeRowNum;
	}

	/**
	 * 
	 * Description:同类别医院的排名
	 * 
	 * @param mapParamter
	 *            (key)coding 医院编码 和 查询时间 nowDate yyyy-MM
	 * @return int
	 * 
	 *         Modification History: Date Version Author Description
	 *         ------------
	 *         ------------------------------------------------------ 2015-3-11
	 *         mhis_hcpms EX-TIANZINONG001 创建
	 */
	@Override
	public int getClassesRowNum(Map<String, Object> mapParamter) {
		// 两个参数
		int classesRowNum = commonDAO.count("classesRowNum.query", mapParamter);
		return classesRowNum;
	}

	/**
	 * 
	 * Description: 同类别医院的总数
	 * 
	 * @param mapParamter
	 *            (key)coding 医院编码 和 查询时间 nowDate yyyy-MM
	 * @return int
	 * 
	 *         Modification History: Date Version Author Description
	 *         ------------
	 *         ------------------------------------------------------ 2015-3-11
	 *         mhis_hcpms EX-TIANZINONG001 创建
	 */
	@Override
	public int getClassesCount(Map<String, Object> mapParamter) {
		// 两个参数
		int classesRowNum = commonDAO.count("classesCount.query", mapParamter);
		return classesRowNum;
	}

	/**
	 * 
	 * Description: 获取指标体系编号
	 * 
	 * @param map
	 *            根据医院的统筹区编码regioncode和医院等级grade
	 * @return String
	 * 
	 *         Modification History: Date Version Author Description
	 *         ------------
	 *         ------------------------------------------------------ 2015-3-11
	 *         mhis_hcpms EX-TIANZINONG001 创建
	 */
	// 得到指标体系编码
	@Override
	public String getStructureNo(Map<String, Object> map) {
		Object no = commonDAO.get("structureNo.query", map);
		if (null == no) {
			return (String) commonDAO.get("structureNoCreatedNew.query", map);
		} else {
			return no.toString();
		}
	}

	/**
	 * 
	 * Description:word导出第四部分 fwa结果 三级指标
	 * 
	 * @param coding
	 * @param kpiTime
	 * @return List
	 * 
	 *         Modification History: Date Version Author Description
	 *         ------------
	 *         ------------------------------------------------------ 2015-3-11
	 *         mhis_hcpms EX-TIANZINONG001 创建
	 */
	@Override
	public List<CodeNameAndScore> getCodeNameAndScore(String coding, String kpiTime, String regionCode,
			String structureNo, String grade) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("coding", coding);
		map.put("nowDate", kpiTime);
		map.put("regioncode", regionCode);
		map.put("structureNO", structureNo);
		map.put("grade", grade);
		List<CodeNameAndScore> scoreList = commonDAO.query("codeNameAndScore.query", map);
		if (CollectionUtils.isEmpty(scoreList)) {
			return new ArrayList<CodeNameAndScore>();
		}
		return scoreList;

	}

	@Override
	public Map<String, Object> getForthPartStr(String medicalOrgCode, String startDate, String endDate)
			throws Exception {
		return fwaOrgSao.invokeOrgInfoESA(medicalOrgCode, startDate, endDate);
	}
}
